//
//  myOrder.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/22.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "myOrder.h"
#import "myOdderCell.h"
#import "orderDetial.h"
#import "orderListModel.h"
@interface myOrder ()

@end

@interface  customVC2 : customVC
@property (nonatomic, strong) NSMutableArray *orderList;

@end
@implementation myOrder

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的订单";
}
- (void)setUpAllViewController {
    customVC2 *VC1 = [customVC2 new];
    customVC2 *VC2 = [customVC2 new];
    customVC2 *VC3 = [customVC2 new];
    VC1.title = @"全部";
    VC2.title = @"分期";
    VC3.title = @"全额";
    [self addChildViewController:VC1];
    [self addChildViewController:VC2];
    [self addChildViewController:VC3];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

#pragma mark --- List
@implementation customVC2
- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccessfulAction) name:@"paySuccessful" object:nil];
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 35);
    self.tableView = [[UITableView alloc]initWithFrame:frame];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    NSLog(@"self.title = %@", self.title);
    [self addRefreshHeader];
    [self addRefreshFooter];
    [self confingureTableView];
    [self.tableView.mj_header beginRefreshing];
}
- (void)paySuccessfulAction {
    [self loadNew];
}
// MARK: - 下拉刷新
- (void)addRefreshHeader {
    [super addRefreshHeader];
}
- (void)addRefreshFooter {
    [super addRefreshFooter];
}

- (void)loadNew
{
    self.page = 1;
    [self orderListRequest];
}

- (void)loadMore
{
    if (!self.orderList.count) {
        [self endFooterRefresh];
        return;
    }
    self.page = self.page + 1;
    [self orderListRequest];
}
- (void)confingureTableView {
    // [super confingureTableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([myOdderCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([myOdderCell class])];
}
- (void)handleResponseData:(NSDictionary *)responseObject
{
    NSMutableArray *modelArray =  [orderListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"result"]];
    if (self.page == 1) {
        [self.orderList removeAllObjects];
    }
    if (modelArray.count > 0) {
        [self.orderList addObjectsFromArray:(NSArray *)modelArray];
        if (modelArray.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }else {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}
#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.orderList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    myOdderCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([myOdderCell class])];
    cell.model = [self.orderList objectAtIndex:indexPath.section];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    orderDetial *VC = [orderDetial new];
    orderListModel *model = [self.orderList objectAtIndex:indexPath.section];
    VC.Id = model.id;
    [self.navigationController pushViewController:VC animated:YES];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    grayView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return grayView;
}
- (void)orderListRequest {
    NSString *orderType;
    if ([self.title isEqualToString:@"全部"]) {
        orderType = @"";
    }else if ([self.title isEqualToString:@"分期"]) {
        orderType = @"2";
    }else if ([self.title isEqualToString:@"全额"]) {
        orderType = @"1";
    }
    NSDictionary *dic = @{
                          @"pager.pageNumber":@(self.page),
                          @"pager.pageSize":@10,
                          @"token":USERTOKEN,
                          @"orderType":orderType
                          };
      __weak typeof(self) weakSelf = self;
    [networkManger PostHttpDataWithUrlStr:[[apiManger shareApiManger] orderList] Dic:dic SuccessBlock:^(id responseObject) {
        [weakSelf endRefreshing];
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf handleResponseData:responseObject];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [weakSelf endRefreshing];
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
#pragma mark --- getter
- (NSMutableArray *)orderList {
    if (!_orderList) {
        _orderList = [NSMutableArray array];
    }
    return _orderList;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
